<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账号" prop="bankAccount">
        <el-input
          v-model="queryParams.bankAccount"
          placeholder="请输入银行账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户银行" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入开户银行"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急联系人" prop="emergencyContact">
        <el-input
          v-model="queryParams.emergencyContact"
          placeholder="请输入紧急联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['loan:auth:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['loan:auth:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['loan:auth:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['loan:auth:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="authList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="authId" />
      <el-table-column label="关联用户ID" align="center" prop="userId" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="身份证正面照" align="center" prop="idCardFront" />
      <el-table-column label="身份证反面照" align="center" prop="idCardBack" />
      <el-table-column label="银行账号" align="center" prop="bankAccount" />
      <el-table-column label="开户银行" align="center" prop="bankName" />
      <el-table-column label="开户支行" align="center" prop="bankBranch" />
      <el-table-column label="紧急联系人" align="center" prop="emergencyContact" />
      <el-table-column label="紧急联系电话" align="center" prop="emergencyPhone" />
      <el-table-column label="居住地址" align="center" prop="residentialAddress" />
      <el-table-column label="户籍地址" align="center" prop="domicileAddress" />
      <el-table-column label="工作单位" align="center" prop="companyName" />
      <el-table-column label="职位" align="center" prop="jobPosition" />
      <el-table-column label="工作年限" align="center" prop="workYears" />
      <el-table-column label="安全问题1" align="center" prop="securityQuestion1" />
      <el-table-column label="答案1" align="center" prop="securityAnswer1" />
      <el-table-column label="安全问题2" align="center" prop="securityQuestion2" />
      <el-table-column label="答案2" align="center" prop="securityAnswer2" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['loan:auth:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['loan:auth:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户扩展信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="身份证正面照" prop="idCardFront">
          <el-input v-model="form.idCardFront" placeholder="请输入身份证正面照" />
        </el-form-item>
        <el-form-item label="身份证反面照" prop="idCardBack">
          <el-input v-model="form.idCardBack" placeholder="请输入身份证反面照" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="开户银行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="开户支行" prop="bankBranch">
          <el-input v-model="form.bankBranch" placeholder="请输入开户支行" />
        </el-form-item>
        <el-form-item label="紧急联系人" prop="emergencyContact">
          <el-input v-model="form.emergencyContact" placeholder="请输入紧急联系人" />
        </el-form-item>
        <el-form-item label="紧急联系电话" prop="emergencyPhone">
          <el-input v-model="form.emergencyPhone" placeholder="请输入紧急联系电话" />
        </el-form-item>
        <el-form-item label="居住地址" prop="residentialAddress">
          <el-input v-model="form.residentialAddress" placeholder="请输入居住地址" />
        </el-form-item>
        <el-form-item label="户籍地址" prop="domicileAddress">
          <el-input v-model="form.domicileAddress" placeholder="请输入户籍地址" />
        </el-form-item>
        <el-form-item label="工作单位" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入工作单位" />
        </el-form-item>
        <el-form-item label="职位" prop="jobPosition">
          <el-input v-model="form.jobPosition" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="工作年限" prop="workYears">
          <el-input v-model="form.workYears" placeholder="请输入工作年限" />
        </el-form-item>
        <el-form-item label="安全问题1" prop="securityQuestion1">
          <el-input v-model="form.securityQuestion1" placeholder="请输入安全问题1" />
        </el-form-item>
        <el-form-item label="答案1" prop="securityAnswer1">
          <el-input v-model="form.securityAnswer1" placeholder="请输入答案1" />
        </el-form-item>
        <el-form-item label="安全问题2" prop="securityQuestion2">
          <el-input v-model="form.securityQuestion2" placeholder="请输入安全问题2" />
        </el-form-item>
        <el-form-item label="答案2" prop="securityAnswer2">
          <el-input v-model="form.securityAnswer2" placeholder="请输入答案2" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAuth, getAuth, delAuth, addAuth, updateAuth } from "@/api/loan/auth";

export default {
  name: "Auth",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户扩展信息表格数据
      authList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        realName: null,
        idCard: null,
        bankAccount: null,
        bankName: null,
        emergencyContact: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "关联用户ID不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户扩展信息列表 */
    getList() {
      this.loading = true;
      listAuth(this.queryParams).then(response => {
        this.authList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        authId: null,
        userId: null,
        realName: null,
        idCard: null,
        idCardFront: null,
        idCardBack: null,
        bankAccount: null,
        bankName: null,
        bankBranch: null,
        emergencyContact: null,
        emergencyPhone: null,
        residentialAddress: null,
        domicileAddress: null,
        companyName: null,
        jobPosition: null,
        workYears: null,
        securityQuestion1: null,
        securityAnswer1: null,
        securityQuestion2: null,
        securityAnswer2: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.authId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户扩展信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const authId = row.authId || this.ids
      getAuth(authId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户扩展信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.authId != null) {
            updateAuth(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAuth(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const authIds = row.authId || this.ids;
      this.$modal.confirm('是否确认删除用户扩展信息编号为"' + authIds + '"的数据项？').then(function() {
        return delAuth(authIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('loan/auth/export', {
        ...this.queryParams
      }, `auth_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
